package net.huashitong.oversee.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.oversee.entity.Coordinate;
import net.huashitong.oversee.filter.CoordinateFilter;
import net.huashitong.user.entity.BnpUser;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CoordinateDao extends StringDao<Coordinate> {
    public List<Coordinate> queryAllCoordinate(CoordinateFilter filter) {
        GenericQuery query = createQuery("obj");
        //查询所有问题
        return query.listResult(filter);
    }

    public List<Coordinate> queryCoordinate(CoordinateFilter filter) {
        GenericQuery query = createQuery("obj");
        if (filter.getBaseInfo() != null && !"".equals(filter.getBaseInfo()))
            query.append(" and baseInfo like:baseInfo").setParam("baseInfo", "%" + filter.getBaseInfo() + "%");
        if (filter.getProjectName() != null && !"".equals(filter.getProjectName()))
            query.append(" and projectName like:projectName").setParam("projectName", "%" + filter.getProjectName() + "%");
        if (filter.getContact() != null && !"".equals(filter.getContact()))
            query.append(" and contact =:contact").setParam("contact", filter.getContact());
        if (filter.getHelpInfo() != null && !"".equals(filter.getHelpInfo()))
            query.append(" and helpInfo like:helpInfo").setParam("helpInfo", "%" + filter.getHelpInfo() + "%");
        if (filter.getInvolveUnit() != null && !"".equals(filter.getInvolveUnit()))
            query.append(" and involveUnit =:involveUnit").setParam("involveUnit", filter.getInvolveUnit());
        if (filter.getResDept() != null && !"".equals(filter.getResDept()))
            query.append(" and resDept =:resDept").setParam("resDept", filter.getResDept());
        if (filter.getSysId() != null && !"".equals(filter.getSysId()))
            query.append(" and sysId =:sysId").setParam("sysId", filter.getSysId());
        if (filter.getReportUser() != null && !"".equals(filter.getReportUser()))
            query.append(" and reportUser =:reportUser").setParam("reportUser", filter.getReportUser());
        if (filter.getReportTime() != null && !"".equals(filter.getReportTime()))
            query.append(" and reportTime =:reportTime").setParam("reportTime", filter.getReportTime());

        if (filter.getBoss() != null && !"".equals(filter.getBoss()))
            query.append(" and boss like:boss").setParam("boss",
                    StringUtils.getLikewords(filter.getBoss()));
        if (filter.getResUser() != null && !"".equals(filter.getResUser()))
            query.append(" and resUser like:resUser").setParam("resUser",
                    StringUtils.getLikewords(filter.getResUser()));
        if (filter.getOperator() != null && !"".equals(filter.getOperator()))
            query.append(" and operator like:operator").setParam("operator",
                    StringUtils.getLikewords(filter.getOperator()));
        if (filter.getFeedback() != null && !"".equals(filter.getFeedback()))
            query.append(" and feedback like:feedback").setParam("feedback",
                    StringUtils.getLikewords(filter.getFeedback()));
        if (!"2".equals(filter.getBranchType())) {
            query.append(" and obj.reportUser=:reportUser").setParam("reportUser", filter.getUserinfo())
                    .append(" or obj.reportUserPhone=:reportUserPhone").setParam("reportUserPhone", filter.getUserinfo())
                    .append(" or obj.bossPhone=:bossPhone").setParam("bossPhone", filter.getUserinfo())
                    .append(" or obj.resUserPhone=:resUserPhone").setParam("resUserPhone", filter.getUserinfo())
                    .append(" or obj.operatorPhone=:operatorPhone").setParam("operatorPhone", filter.getUserinfo())
                    .append(" or obj.fGcountyLeaderPhone=:fGcountyLeaderPhone").setParam(
                            "fGcountyLeaderPhone", filter.getUserinfo());

        }

        //模糊搜索
        if(StringUtils.isNotBlank(filter.getSearchWord())){
            query.append(" and (obj.projectName like:projectName or " +
                    "obj.baseInfo like:baseInfo or "+
                    "obj.helpInfo like:helpInfo or "+
                    "obj.contact like:contact or "+
                    "obj.reportUser like:reportUser or "+
                    "obj.fGcountyLeader like:fGcountyLeader or " +
                    "obj.resDept like:resDept or " +
                    "obj.involveUnit like:involveUnit) ").setParam("projectName",
                    StringUtils.getLikewords(filter.getSearchWord())).setParam("baseInfo",
                    StringUtils.getLikewords(filter.getSearchWord())).setParam("helpInfo",
                    StringUtils.getLikewords(filter.getSearchWord())).setParam("contact",
                    StringUtils.getLikewords(filter.getSearchWord())).setParam("reportUser",
                    StringUtils.getLikewords(filter.getSearchWord())).setParam("fGcountyLeader",
                    StringUtils.getLikewords(filter.getSearchWord())).setParam("resDept",
                    StringUtils.getLikewords(filter.getSearchWord())).setParam("involveUnit",
                    StringUtils.getLikewords(filter.getSearchWord()));
        }

        if (filter.getPaging() != null) {
            if (filter.getPaging().getSortCol() == null || "".equals(filter.getPaging().getSortCol())) {
                query.append(" ORDER BY  str_to_date( CONCAT(obj.updDate,' ',obj.updTime),'%Y-%m-%d %H:%i:%s') desc  ");
            }
        }
        //query.append(" and obj.aaa001='1'");
        return query.listResult(filter);
    }


    public int queryCoordinateCount(CoordinateFilter filter) {

        GenericQuery query = createQuery("obj");
        if (filter.getBaseInfo() != null && !"".equals(filter.getBaseInfo()))
            query.append(" and baseInfo like:baseInfo").setParam("baseInfo", "%" + filter.getBaseInfo() + "%");
        if (filter.getProjectName() != null && !"".equals(filter.getProjectName()))
            query.append(" and projectName like:projectName").setParam("projectName", "%" + filter.getProjectName() + "%");
        if (filter.getContact() != null && !"".equals(filter.getContact()))
            query.append(" and contact =:contact").setParam("contact", filter.getContact());
        if (filter.getHelpInfo() != null && !"".equals(filter.getHelpInfo()))
            query.append(" and helpInfo like:helpInfo").setParam("helpInfo", "%" + filter.getHelpInfo() + "%");
        if (filter.getInvolveUnit() != null && !"".equals(filter.getInvolveUnit()))
            query.append(" and involveUnit =:involveUnit").setParam("involveUnit", filter.getInvolveUnit());
        if (filter.getResDept() != null && !"".equals(filter.getResDept()))
            query.append(" and resDept =:resDept").setParam("resDept", filter.getResDept());
        if (filter.getSysId() != null && !"".equals(filter.getSysId()))
            query.append(" and sysId =:sysId").setParam("sysId", filter.getSysId());
        if (filter.getReportUser() != null && !"".equals(filter.getReportUser()))
            query.append(" and reportUser =:reportUser").setParam("reportUser", filter.getReportUser());
        if (filter.getReportTime() != null && !"".equals(filter.getReportTime()))
            query.append(" and reportTime =:reportTime").setParam("reportTime", filter.getReportTime());

        if (filter.getBoss() != null && !"".equals(filter.getBoss()))
            query.append(" and boss like:boss").setParam("boss",
                    StringUtils.getLikewords(filter.getBoss()));
        if (filter.getResUser() != null && !"".equals(filter.getResUser()))
            query.append(" and resUser like:resUser").setParam("resUser",
                    StringUtils.getLikewords(filter.getResUser()));
        if (filter.getOperator() != null && !"".equals(filter.getOperator()))
            query.append(" and operator like:operator").setParam("operator",
                    StringUtils.getLikewords(filter.getOperator()));
        if (filter.getFeedback() != null && !"".equals(filter.getFeedback()))
            query.append(" and feedback like:feedback").setParam("feedback",
                    StringUtils.getLikewords(filter.getFeedback()));
        if (!"2".equals(filter.getBranchType())) {
            query.append(" and obj.reportUser=:reportUser").setParam("reportUser", filter.getUserinfo())
                    .append(" or obj.reportUserPhone=:reportUserPhone").setParam("reportUserPhone", filter.getUserinfo())
                    .append(" or obj.bossPhone=:bossPhone").setParam("bossPhone", filter.getUserinfo())
                    .append(" or obj.resUserPhone=:resUserPhone").setParam("resUserPhone", filter.getUserinfo())
                    .append(" or obj.operatorPhone=:operatorPhone").setParam("operatorPhone", filter.getUserinfo());

        }

        if (filter.getPaging() != null) {
            if (filter.getPaging().getSortCol() == null || "".equals(filter.getPaging().getSortCol())) {
                query.append(" ORDER BY  str_to_date( CONCAT(obj.updDate,' ',obj.updTime),'%Y-%m-%d %H:%i:%s') desc  ");
            }
        }

        List<Coordinate> list = query.listResult();
        if (ObjectUtils.isNotBlank(list)) {
            return list.size();
        } else {
            return 0;
        }
    }

    //我的问题反馈
    public List<Coordinate> getMyQusetion(BnpUser user) {
        GenericQuery query = createQuery(" obj");
        query.append(" and obj.reportUser=:reportUser").setParam("reportUser", user.getPhone())
                .append(" or obj.reportUserPhone=:reportUserPhone").setParam("reportUserPhone", user.getPhone())
                .append(" or obj.bossPhone=:bossPhone").setParam("bossPhone", user.getPhone())
                .append(" or obj.resUserPhone=:resUserPhone").setParam("resUserPhone", user.getPhone())
                .append(" or obj.operatorPhone=:operatorPhone").setParam("operatorPhone", user.getPhone());
        // .append(" or obj.groupCode like: groupCode").setParam("groupCode",StringUtils.getLikewords(user.getGroupCode()));
        return query.listResult();
    }


    public List<Coordinate> queryCoordinates(CoordinateFilter filter){
        GenericQuery query = createQuery("obj");
        if (filter.getNodeSysId() != null && !"".equals(filter.getNodeSysId()))
            query.append(" and nodeSysId =:nodeSysId").setParam("nodeSysId", filter.getNodeSysId());
        if (filter.getBaseInfo() != null && !"".equals(filter.getBaseInfo()))
            query.append(" and baseInfo like:baseInfo").setParam("baseInfo", "%" + filter.getBaseInfo() + "%");
        if (filter.getProjectName() != null && !"".equals(filter.getProjectName()))
            query.append(" and projectName like:projectName").setParam("projectName", "%" + filter.getProjectName() + "%");
        if (filter.getContact() != null && !"".equals(filter.getContact()))
            query.append(" and contact =:contact").setParam("contact", filter.getContact());
        if (filter.getHelpInfo() != null && !"".equals(filter.getHelpInfo()))
            query.append(" and helpInfo like:helpInfo").setParam("helpInfo", "%" + filter.getHelpInfo() + "%");
        if (filter.getInvolveUnit() != null && !"".equals(filter.getInvolveUnit()))
            query.append(" and involveUnit =:involveUnit").setParam("involveUnit", filter.getInvolveUnit());
        if (filter.getResDept() != null && !"".equals(filter.getResDept()))
            query.append(" and resDept =:resDept").setParam("resDept", filter.getResDept());
        if (filter.getSysId() != null && !"".equals(filter.getSysId()))
            query.append(" and sysId =:sysId").setParam("sysId", filter.getSysId());
        if (filter.getReportUser() != null && !"".equals(filter.getReportUser()))
            query.append(" and reportUser =:reportUser").setParam("reportUser", filter.getReportUser());
        if (filter.getReportTime() != null && !"".equals(filter.getReportTime()))
            query.append(" and reportTime =:reportTime").setParam("reportTime", filter.getReportTime());
        if (filter.getBoss() != null && !"".equals(filter.getBoss()))
            query.append(" and boss like:boss").setParam("boss",
                    StringUtils.getLikewords(filter.getBoss()));
        if (filter.getResUser() != null && !"".equals(filter.getResUser()))
            query.append(" and resUser like:resUser").setParam("resUser",
                    StringUtils.getLikewords(filter.getResUser()));
        if (filter.getOperator() != null && !"".equals(filter.getOperator()))
            query.append(" and operator like:operator").setParam("operator",
                    StringUtils.getLikewords(filter.getOperator()));
        if (filter.getFeedback() != null && !"".equals(filter.getFeedback()))
            query.append(" and feedback like:feedback").setParam("feedback",
                    StringUtils.getLikewords(filter.getFeedback()));
        return query.listResult();
    }

}
